SQL 用法–postgresql
[TOC]
运算符
https://www.postgresql.org/docs/9.1/static/functions-geometry.html
官方链接最全。
表格操作
建表
1 | CREATE TABLE users ( |
改表
1 | ALTER TABLE table_name |
删表
TRUNCATE table_name; // 将行清空,但表头依旧保留
DROP table table_name; //删除整个表格
PRIMARY KEY constraint | UNIQUE constraint | |
---|---|---|
The number of constraints | One | Many |
NULL values | Do not allow | Allow |
表的连接
内连接–INNER JOIN
两个表使用内连接,通过指定一个属性来匹配,若两个表的同一属性具有相同值,则保留在连接表中,其余不相等的行则消去。
图解
使用示例(内连接也可以连接多张表格)
1 | SELECT |
全连接–FUll OUTER JOIN
指定连接条件,属性值相同的行保留,但不重复,属性值不同的值保留,但存在某些为Null的情况。
用法示例
1 | SELECT column_list |
左连接–LEFT JOIN
对几个将要连接的表指定条件进行连接,属性匹配的时候,会保留左表所有属性,即便右表属性为NULL,但不会存在左表属性为NULL的情况。
代码示例:
1 | SELECT |
右连接–RIGHT JOIN
和左连接相反,用法如下:
1 | SELECT |
交叉连接–CROSS JOIN
看图就知道。。。交叉连接结果的表格是 两表行数相乘。需要指出的一点就是两个表相连不需要指定cross join。
1 | SELECT |
创建索引
PostgreSQL provides several index types: B-tree, Hash, GiST and GIN. Each index type uses a different algorithm that is best suited to different types of queries. By default, the
CREATE INDEX
command creates B-tree indexes, which fit the most common situations.
创建b_tree索引
语法: create index index_name ON table_name(column_name)
postgresql默认b树索引。官方描述如下:
B-trees can handle equality and range queries on data that can be sorted into some ordering. In particular, the PostgreSQL query planner will consider using a B-tree index whenever an indexed column is involved in a comparison using one of these operators:
<
<=
=
>=
>
Constructs equivalent to combinations of these operators, such as
BETWEEN
and
IN
, can also be implemented with a B-tree index search. Also, an
IS NULL
or
IS NOT NULL
condition on an index column can be used with a B-tree index.
上述说明b_tree在范围查询中比较有效,也可以用于顺序检索数据。
当查询条件为范围查询时(运算符是 > < <= >= = is null, is not null, between and ,in),b_tree索引可以体现较为良好的性能。
创建hash索引
语法: create index index_name on table_name using hash(column_name)
官方描述
Hash indexes can only handle simple equality comparisons. The query planner will consider using a hash index whenever an indexed column is involved in a comparison using the
=
operator.
上述说明,hash索引仅仅在处理简单的等值比较-(=) 中相对有效。
使用GiST索引
(错误)语法: create index index_name on table_name using GiST(column_name)
官方描述
GiST indexes are not a single kind of index, but rather an infrastructure within which many different indexing strategies can be implemented. Accordingly, the particular operators with which a GiST index can be used vary depending on the indexing strategy (the operator class). As an example, the standard distribution of PostgreSQL includes GiST operator classes for several two-dimensional geometric data types, which support indexed queries using these operators:
<<
&<
&>
>>
`<< ` `&< ` ` &>` ` >>` @>
<@
~=
&&
上述描述可知,gist 不是单一类型的索引,实现了不同策略索引的基础结构。可以根据索引策略(运算符类)来采取不同的索引策略。
使用GIN索引
gin索引能够用于优化级的最近邻查找。
GIN indexes are inverted indexes which can handle values that contain more than one key, arrays for example. Like GiST, GIN can support many different user-defined indexing strategies and the particular operators with which a GIN index can be used vary depending on the indexing strategy. As an example, the standard distribution of PostgreSQLincludes GIN operator classes for one-dimensional arrays, which support indexed queries using these operators:
<@
@>
=
&&
用法查看官方描述gin index.
分析效率 explain
将explain(analyze)置于语句的开始,结果将返回语句执行效率有关的数值。
1 |
|
1 | Click To Expand Code |